package top.soulblack.purchasesale.controller;



import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import top.soulblack.purchasesale.model.*;
import top.soulblack.purchasesale.model.GoodsBrand;
import top.soulblack.purchasesale.model.GoodsColor;
import top.soulblack.purchasesale.model.GoodsMaterial;
import top.soulblack.purchasesale.model.GoodsSize;
import top.soulblack.purchasesale.model.GoodsUnit;
import top.soulblack.purchasesale.model.Role;
import top.soulblack.purchasesale.model.VO.AnnounceVO;
import top.soulblack.purchasesale.model.VO.GoodsToIdVO;
import top.soulblack.purchasesale.service.*;
import top.soulblack.purchasesale.service.system.AnnounceService;
import top.soulblack.purchasesale.service.system.ShopService;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

/**
 * create by 小川
 */
@Slf4j
@Controller
@RequestMapping("setting")
public class SettingController {

    @Autowired
    private GoodsColorService goodsColorService;

    @Autowired
    private GoodsSizeService goodsSizeService;

    @Autowired
    private GoodsUnitService goodsUnitService;

    @Autowired
    private GoodsBrandService goodsBrandService;

    @Autowired
    private GoodsMaterialService goodsMaterialService;

    @Autowired
    private GoodsStyleService goodsStyleService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private GoodsClassifyService goodsClassifyService;

    @Autowired
    private GoodsSeasonService goodsSeasonService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private AnnounceService announceService;

    @Autowired
    private UserService userService;

    @Autowired
    private BarCodeService barcodeService;

    //商品设置
    @RequestMapping("/commodity_management")
    public String CommodityManagement(Model model, HttpServletRequest request){
        List<GoodsToIdVO> goodsToIdListst = null;
        if (request.getParameter("goodsName") != "" && request.getParameter("goodsName") != null) {
            String goodsName = request.getParameter("goodsName");
            goodsToIdListst = goodsService.selectFromName(goodsName);
        }else {
            goodsToIdListst = goodsService.selectToId();
        }
        model.addAttribute("goods",goodsToIdListst);
        // goodsColors
        List<GoodsColor> goodsColors = null;
        goodsColors = goodsColorService.selectAll();
        model.addAttribute("goodsColors",goodsColors);
        // 查找所有分组
        List<String> groups = goodsColorService.selectGroups();
        model.addAttribute("groups",groups);
        // goodsSizes
        List<GoodsSize> goodsSizes = null;
        goodsSizes = goodsSizeService.selectAll();
        model.addAttribute("goodsSizes",goodsSizes);
        // 查找所有分组
        List<String> size_groups = goodsSizeService.selectGroups();
        model.addAttribute("size_groups",size_groups);
        // goodsBrands
        List<GoodsBrand> goodsBrands = goodsBrandService.selectAll();
        model.addAttribute("goodsBrands",goodsBrands);
        // goodsMaterials
        List<GoodsMaterial> goodsMaterials = goodsMaterialService.selectAll();
        model.addAttribute("goodsMaterials",goodsMaterials);
        // unit
        List<GoodsUnit> goodsUnits = goodsUnitService.selectAll();
        model.addAttribute("goodsUnits", goodsUnits);
        // goodsSeasons
        List<GoodsSeason> goodsSeasons = goodsSeasonService.selectAll();
        model.addAttribute("goodsSeasons",goodsSeasons);
        // barcode
        BarCode barcode=barcodeService.select_max();
        model.addAttribute("barcode",barcode);
        return "setting/commodity/CommodityManagement";
    }

    @RequestMapping("/commodity_category")
    public String CommodityCategory(Model model,HttpServletRequest request){
        List<GoodsClassify> goodsClassifies = null;
        if (request.getParameter("remarks") != "" && request.getParameter("remarks") != null){
            // 查找当前分组颜色
            String group = request.getParameter("remarks");
            goodsClassifies = goodsClassifyService.selectForGroup(group);
        } else {
            // 查找所有颜色
            goodsClassifies = goodsClassifyService.selectAll();
        }
        model.addAttribute("goodsClassify",goodsClassifies);
        // 查找所有分组
        List<String> groups = goodsClassifyService.selectGroups();
        model.addAttribute("groups",groups);
        return "setting/commodity/CommodityCategory";
    }

    @RequestMapping("/color_management")
    public String ColorManagement(HttpServletRequest request, Model model){
        List<GoodsColor> goodsColors = null;
        if (request.getParameter("colorName") != "" && request.getParameter("colorName") != null) {
            // 根据颜色名称查询
            String colorName = request.getParameter("colorName");
            goodsColors = goodsColorService.selectForName(colorName);
        } else if (request.getParameter("groups") != "" && request.getParameter("groups") != null){
            // 查找当前分组颜色
            String group = request.getParameter("groups");
            goodsColors = goodsColorService.selectForGroup(group);
        } else {
            // 查找所有颜色
            goodsColors = goodsColorService.selectAll();
        }
        model.addAttribute("goodsColors",goodsColors);
        // 查找所有分组
        List<String> groups = goodsColorService.selectGroups();
        model.addAttribute("groups",groups);
        return "setting/commodity/ColorManagement";
    }

    @RequestMapping("/size_management")
    public String SizeManagement(HttpServletRequest request,Model model){
        List<GoodsSize> goodsSizes = null;
        if (request.getParameter("sizeName") != "" && request.getParameter("sizeName") != null) {
            // 根据尺码名称查询
            String sizeName = request.getParameter("sizeName");
            goodsSizes = goodsSizeService.selectForName(sizeName);
        } else if (request.getParameter("groups") != "" && request.getParameter("groups") != null){
            // 查找当前分组尺码
            String group = request.getParameter("groups");
            goodsSizes = goodsSizeService.selectForGroup(group);
        } else {
            // 查找所有尺码
            goodsSizes = goodsSizeService.selectAll();
        }
        model.addAttribute("goodsSizes",goodsSizes);
        // 查找所有分组
        List<String> groups = goodsSizeService.selectGroups();
        model.addAttribute("groups",groups);
        return "setting/commodity/SizeManagement"; }

    @RequestMapping("/brand_management")
    public String BrandManagement(Model model){

        List<GoodsBrand> goodsBrands = goodsBrandService.selectAll();
        model.addAttribute("goodsBrands",goodsBrands);
        return "setting/commodity/BrandManagement";
    }

    @RequestMapping("/material_management")
    public String MaterialManagement(Model model){

        List<GoodsMaterial> goodsMaterials = goodsMaterialService.selectAll();
        model.addAttribute("goodsMaterials",goodsMaterials);
        return "setting/commodity/MaterialManagement";
    }

    @RequestMapping("/style_management")
    public String StyleManagement(Model model,HttpServletRequest request){
        List<GoodsStyle> goodsStyles = null;
        if (request.getParameter("classify") != "" && request.getParameter("classify") != null){
            String classifys = request.getParameter("classify");
            Integer classify = Integer.valueOf(classifys).intValue();
            goodsStyles = goodsStyleService.selectForClassify(classify);
        }else{
            goodsStyles = goodsStyleService.selectAll();
        }

        model.addAttribute("goodsStyles",goodsStyles);
        List<String> classify = goodsStyleService.selectclassif();
        model.addAttribute("classify",classify);
        return "setting/commodity/StyleManagement";
    }

    @RequestMapping("/season_management")
    public String SeasonManagement(Model model){
        List<GoodsSeason> goodsSeasons = goodsSeasonService.selectAll();
        model.addAttribute("goodsSeasons",goodsSeasons);
        return "setting/commodity/SeasonManagement";
    }

    @RequestMapping("/unit_management")
    public String UnitManagement(Model model) {
        List<GoodsUnit> goodsUnits = goodsUnitService.selectAll();
        model.addAttribute("goodsUnits", goodsUnits);
        return "setting/commodity/UnitManagement";
    }

    @RequestMapping("/barcode_management")
    public String BarcodeManagement(Model model){
        BarCode barcode=barcodeService.selectbarcode();
        model.addAttribute("barcode",barcode);
        return "setting/commodity/BarCodeManagement";
    }


    //系统设置
    @RequestMapping("/person_info")
    public String PersonInfo(Model model){
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);
        return "setting/system/PersonInfo";
    }

    // 门店与人员管理
    @RequestMapping("/stores_and_employees")
    public String StoresAndEmployees(HttpServletRequest request, Model model){
        List<User> users = null;
        if(request.getParameter("shops") != "" && request.getParameter("shops") != null){
            String shops = request.getParameter("shops");
            users = userService.selectForShop(shops);
        }
        else{
            users = userService.findAll();
        }
        model.addAttribute("users",users);
        List<Shop> shops = shopService.selectAll();
        model.addAttribute("shops", shops);
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);
        return "setting/system/StoresAndEmployees";
    }

    @RequestMapping("/role_management")
    public String RoleManagement(HttpServletRequest request, Model model){
        List<Role> role = null;
        role = roleService.selectAll();
        model.addAttribute("role",role);

        return "setting/system/RoleManagement";
    }

    @RequestMapping("/company_announcement")
    public String CompanyAnnouncement(Model model,HttpServletRequest request){

        List<AnnounceVO> announceVOS = announceService.selectRole();
        if (request.getParameter("title") != "" && request.getParameter("title") != null){
            String announceTitle = request.getParameter("title");
            List<SystemAnnounce> announces = announceService.selectForTitle(announceTitle);
            model.addAttribute("announces",announces);
        }
        model.addAttribute("systemAnnounces",announceVOS);
        return "setting/system/CompanyAnnouncement";
    }

    @RequestMapping("/logistics_management")
    public String LogisticsManagement(){
        return "setting/system/LogisticsManagement";
    }

    @RequestMapping("/system_parameters")
    public String SystemParameters(){
        return "setting/system/SystemParameters";
    }

    @RequestMapping("/system_log")
    public String SystemLog(){
        return "setting/system/SystemLog";
    }

    @RequestMapping("/quick_start")
    public String QuickStarts(){
        return "setting/system/QuickStart";
    }

    @RequestMapping("/print_model")
    public String PrintModel(){
        return "setting/system/PrintModel";
    }
}
